#include <iostream>
#include <fstream>
#include "contacts.pb.h"

const std::string filename = "contacts.bin";

void AddPeopleInfo(contacts::PeopleInfo* people)
{
    std::cout << "请输入联系人姓名:";
    std::string name;
    std::getline(std::cin, name);
    people->set_name(name);

    std::cout << "请输入联系人年龄:";
    int age;
    std::cin >> age;
    people->set_age(age);

    std::cin.ignore(256, '\n'); // 清空缓冲区信息
    for (int i = 0; ; i++)
    {
        std::cout << "请输入联系人电话" << i + 1 << "(输入回车完成电话新增):";
        std::string number;
        std::getline(std::cin, number);
        if (number.empty())
            break;
        contacts::PeopleInfo_Phone* phone = people->add_phones();
        phone->set_number(number);

        std::cout << "请输入电话类型(1.移动电话  2.固定电话):";
        int type;
        std::cin >> type;
        std::cin.ignore(256, '\n'); // 去除回车
        switch(type)
        {
            case 1:
                phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                std::cout << "选择有误!" << std::endl;
                break;
        }


    }

    std::cout << "请输入联系人家庭地址:";
    std::string home_address;
    std::getline(std::cin, home_address);

    std::cout << "请输入联系人单位地址:";
    std::string unit_address;
    std::getline(std::cin, unit_address);

    contacts::Address address;
    address.set_home_address(home_address);
    address.set_unit_address(unit_address);
    people->mutable_data()->PackFrom(address);

    std::cout << "请选择新增联系人的其他联系方式(1.QQ  2.WeChat):";
    int op;
    std::cin >> op;
    std::cin.ignore(256, '\n');
    if (op == 1)
    {
        std::cout << "请输入联系人QQ:";
        std::string qq;
        std::getline(std::cin, qq);
        people->set_qq(qq);
    }
    else if (op == 2)
    {
        std::cout << "请输入联系人WeChat:";
        std::string wechat;
        std::getline(std::cin, wechat);
        people->set_wechat(wechat);
    }
    else{
        std::cout << "选择有误,添加失败!" << std::endl;
    }

    for (int i = 0; ; i++)
    {
        std::cout << "请输入备注" << i + 1 << "标题(只输入回车退出):";
        std::string remark_key;
        std::getline(std::cin, remark_key);
        if (remark_key.empty())
            break;
        std::cout << "请输入备注" << i + 1 << "信息:";
        std::string remark_value;
        std::getline(std::cin, remark_value);
        people->mutable_remark()->insert({remark_key, remark_value});
    }

    std::cout << "添加联系人成功..." << std::endl;
}

int main()
{
    contacts::Contacts contacts;
    std::fstream input(filename, std::ios::in | std::ios::binary);
    if (!input)
    {
        std::cout << filename << " not found, create new file!" << std::endl;
    }
    else if (!contacts.ParseFromIstream(&input))
    {
        std::cout << "parse error..." << std::endl;
        input.close();
        return 2;
    }

    AddPeopleInfo(contacts.add_contacts());

    std::fstream output(filename, std::ios::out | std::ios::trunc | std::ios::binary);
    if (!contacts.SerializePartialToOstream(&output))
    {
        std::cout << "serialize error..." << std::endl;
        input.close();
        output.close();
        return 3;
    }

    std::cout << "write success..." << std::endl;
    input.close();
    output.close();
    return 0;
}